package com.ruoyi.project.module.gs;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.project.domain.*;
import com.ruoyi.project.service.*;
import com.ruoyi.system.api.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 成果信息Controller
 * 
 * @author ruoyi
 * @date 2023-05-10
 */
@RestController
@RequestMapping("/dashuju")
public class GsCxcyAchievementController extends BaseController
{
    @Autowired
    private ICxcyAchievementService cxcyAchievementService;
    @Autowired
    private ICxcyContractService cxcyContractService;
    @Autowired
    private ICxcyExpenseService cxcyExpenseService;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private ICxcyTransformationService cxcyTransformationService;
    /**
     * 查询成果信息列表
     */
   /* @RequiresPermissions("project:dashujuachievement:list")*/
    @GetMapping("/list")
    public TableDataInfo list(CxcyAchievement cxcyAchievement)
    {

        List<CxcyAchievement> list = cxcyAchievementService.selectCxcyAchievementList(cxcyAchievement);
        return getDataTable(list);
    }

    /**
     * 导出成果信息列表
     */
    /*@RequiresPermissions("project:dashujuachievement:export")*/
    @Log(title = "成果信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CxcyAchievement cxcyAchievement)
    {
        List<CxcyAchievement> list = cxcyAchievementService.selectCxcyAchievementList(cxcyAchievement);
        ExcelUtil<CxcyAchievement> util = new ExcelUtil<CxcyAchievement>(CxcyAchievement.class);
        util.exportExcel(response, list, "成果信息数据");
    }

    /**
     * 获取成果信息详细信息
     */
   /* @RequiresPermissions("project:dashujuachievement:query")*/
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(cxcyAchievementService.selectCxcyAchievementById(id));
    }

    /**
     * 新增成果信息
     */
   /* @RequiresPermissions("project:dashujuachievement:add")*/
    @Log(title = "成果信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CxcyAchievement cxcyAchievement)
    {
        return toAjax(cxcyAchievementService.insertCxcyAchievement(cxcyAchievement));
    }

    /**
     * 修改成果信息
     */
   /* @RequiresPermissions("project:achievement:edit")*/
    @Log(title = "成果信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CxcyAchievement cxcyAchievement)
    {
        return toAjax(cxcyAchievementService.updateCxcyAchievement(cxcyAchievement));
    }

    /**
     * 删除成果信息
     */
   /* @RequiresPermissions("project:achievement:remove")*/
    @Log(title = "成果信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(cxcyAchievementService.deleteCxcyAchievementByIds(ids));
    }

    /**
     * 查询成果总数
     * @return
     */



    @GetMapping("/getListData")
    public String[][]  getData(CxcyAchievement cxcyAchievement) {



        List<CxcyAchievement> list = cxcyAchievementService.selectCxcyAchievementList(cxcyAchievement);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String[][] data = new String[0][];
        for(int i=0;i<list.size();i++){

            String applyTime = dateFormat.format(list.get(i).getApplyTime());
            String[] rowData = {

                    applyTime,
                    String.valueOf(list.get(i).getResultName()),
                    String.valueOf(list.get(i).getFirstCollege()),
                    String.valueOf(list.get(i).getFirstName())

            };
            // 扩展数组大小并将字符串值赋给新数组
            data = Arrays.copyOf(data, data.length + 1);
            data[data.length - 1] = rowData;
        }


        return data;
    }






    /**
     * 查询成果分布
     * @return
     */

    @GetMapping("/fenbu")
    public Map<String, Object> fenbulist(CxcyAchievement cxcyAchievement)
    {

        //创新训练项目总成果数
        CxcyAchievement cxcyAchievement1=new CxcyAchievement();
        cxcyAchievement1.setResultType("0");
        List<CxcyAchievement> list=  cxcyAchievementService.selectCxcyAchievementList(cxcyAchievement1);
        int cxtotal=list.size();
        System.out.println(cxtotal+"cxtotal");

        //创业训练项目总成果数
        CxcyAchievement cxcyAchievement2=new CxcyAchievement();
        cxcyAchievement2.setResultType("1");
        List<CxcyAchievement> list1=  cxcyAchievementService.selectCxcyAchievementList(cxcyAchievement2);
        int cytotal=list1.size();
        System.out.println(cytotal+"cytotal");

        //创业实践项目总成果数
        CxcyAchievement cxcyAchievement3=new CxcyAchievement();
        cxcyAchievement3.setResultType("1");
        List<CxcyAchievement> list2=  cxcyAchievementService.selectCxcyAchievementList(cxcyAchievement3);
        int cysjtotal=list2.size();
        System.out.println(cysjtotal+"cysjtotal");

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("cxtotal", cxtotal);
        resultMap.put("cytotal",cytotal);
        resultMap.put("cysjtotal", cysjtotal);

        return resultMap;

    }

    /**
     * 查询成果总数
     * @return
     */

    @GetMapping("/zongshu")
    public Map<String, Object> zonglist(CxcyAchievement cxcyAchievement)
    {
        //本年度总成果数
        List<CxcyAchievement> list=  cxcyAchievementService.selectCxcyAchievementList(cxcyAchievement);
        int resulttotal=list.size();
        System.out.println(resulttotal+"resulttotal");


        //本年度转化总成果数
        CxcyContract cxcyContract=new CxcyContract();
        cxcyContract.setAuditState2("2");
        List<CxcyContract> list1 = cxcyContractService.selectCxcyContractList(cxcyContract);
        int zhuanresulttotal=list1.size();
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("resulttotal", resulttotal);
        resultMap.put("zhuanresulttotal",zhuanresulttotal);
        return resultMap;
    }
    @GetMapping("/jiner")
    public Map<String, Object> jinerlist(CxcyExpense cxcyExpense1)
    {




        List<CxcyExpense> list1=  cxcyExpenseService.selectCxcyExpenseList(cxcyExpense1);
        int totalmoney=0;

for(CxcyExpense cx:list1){
    System.out.println(cx.getRemittingPrice()+"qqqqqqqqqqqqqqqqqqqqq");
    if (cx.getRemittingPrice() == null) {
        totalmoney+= 0;
    }else{
        totalmoney+= Integer.valueOf(cx.getRemittingPrice());
    }

}
        System.out.println(totalmoney+"totalmoney");



        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("totalmoney",totalmoney);
        return resultMap;

    }



    @GetMapping("/listOne")
    public Map<String, Object> listOne(CxcyAchievement cxcyAchievement)
    {//2023
        int cx=0;
        int cy=0;
        int sj=0;
        CxcyAchievement cxcyAchievementcx=new CxcyAchievement();
        cxcyAchievementcx.setResultType("0");
        cxcyAchievementcx.setNote8("2023");
        List<CxcyAchievement> list = cxcyAchievementService.selectCxcyAchievementListone(cxcyAchievementcx);
        cx=list.size();
        System.out.println(cx+"2023cx");
        CxcyAchievement cxcyAchievementcy=new CxcyAchievement();
        cxcyAchievementcy.setResultType("1");
        cxcyAchievementcy.setNote8("2023");
        List<CxcyAchievement> list1 = cxcyAchievementService.selectCxcyAchievementListone(cxcyAchievementcy);
        cy=list1.size();
        System.out.println(cy+"2023cy");
        CxcyAchievement cxcyAchievementsj=new CxcyAchievement();
        cxcyAchievementsj.setResultType("2");
        cxcyAchievementsj.setNote8("2023");
        List<CxcyAchievement> list2 = cxcyAchievementService.selectCxcyAchievementListone(cxcyAchievementsj);
        sj=list2.size();
        System.out.println(sj+"2023sj");
//2022
        int twocx=0;
        int twocy=0;
        int twosj=0;
        CxcyAchievement twocxcyAchievementcx=new CxcyAchievement();
        twocxcyAchievementcx.setResultType("0");
        twocxcyAchievementcx.setNote8("2022");
        List<CxcyAchievement> twolist = cxcyAchievementService.selectCxcyAchievementListone(twocxcyAchievementcx);
        twocx=twolist.size();
        System.out.println(twocx+"2022twocx");
        CxcyAchievement twocxcyAchievementcy=new CxcyAchievement();
        twocxcyAchievementcy.setResultType("1");
        twocxcyAchievementcy.setNote8("2022");
        List<CxcyAchievement> twolist1 = cxcyAchievementService.selectCxcyAchievementListone(twocxcyAchievementcy);
        twocy=twolist1.size();
        System.out.println(twocy+"2022twocy");
        CxcyAchievement twocxcyAchievementsj=new CxcyAchievement();
        twocxcyAchievementsj.setResultType("2");
        twocxcyAchievementsj.setNote8("2022");
        List<CxcyAchievement> twolist2 = cxcyAchievementService.selectCxcyAchievementListone(twocxcyAchievementsj);
        twosj=twolist2.size();
        System.out.println(twosj+"2022twosj");
        //2021
        int threecx=0;
        int threecy=0;
        int threesj=0;
        CxcyAchievement threecxcyAchievementcx=new CxcyAchievement();
        threecxcyAchievementcx.setResultType("0");
        threecxcyAchievementcx.setNote8("2021");
        List<CxcyAchievement> threelist = cxcyAchievementService.selectCxcyAchievementListone(threecxcyAchievementcx);
        threecx=threelist.size();
        System.out.println(threecx+"2021twosj");
        CxcyAchievement threecxcyAchievementcy=new CxcyAchievement();
        threecxcyAchievementcy.setResultType("1");
        threecxcyAchievementcy.setNote8("2021");
        List<CxcyAchievement> threelist1 = cxcyAchievementService.selectCxcyAchievementListone(threecxcyAchievementcx);
        threecy=threelist1.size();
        System.out.println(threecy+"2021threecy");
        CxcyAchievement threecxcyAchievementsj=new CxcyAchievement();
        threecxcyAchievementsj.setResultType("2");
        threecxcyAchievementsj.setNote8("2021");

        List<CxcyAchievement> threelist2 = cxcyAchievementService.selectCxcyAchievementListone(threecxcyAchievementcx);
        threesj=threelist2.size();
        System.out.println(threesj+"2021threesj");
//2020

        int fourcx=0;
        int fourcy=0;
        int foursj=0;
        CxcyAchievement fourcxcyAchievementcx=new CxcyAchievement();
        fourcxcyAchievementcx.setResultType("0");
        fourcxcyAchievementcx.setNote8("2020");
        List<CxcyAchievement> fourlist = cxcyAchievementService.selectCxcyAchievementListone(fourcxcyAchievementcx);
        fourcx=fourlist.size();
        System.out.println(fourcx+"2020fourcx");
        CxcyAchievement fourcxcyAchievementcy=new CxcyAchievement();
        fourcxcyAchievementcy.setResultType("1");
        fourcxcyAchievementcy.setNote8("2020");
        List<CxcyAchievement> fourlist1 = cxcyAchievementService.selectCxcyAchievementListone(fourcxcyAchievementcx);
        fourcy=fourlist1.size();
        System.out.println(fourcy+"2020fourcy");
        CxcyAchievement fourcxcyAchievementsj=new CxcyAchievement();
        fourcxcyAchievementsj.setResultType("2");
        fourcxcyAchievementsj.setNote8("2020");
        List<CxcyAchievement> fourlist2 = cxcyAchievementService.selectCxcyAchievementListone(fourcxcyAchievementcx);
        foursj=fourlist2.size();
        System.out.println(foursj+"2020foursj");








        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("cx", cx);
        resultMap.put("cy",cy);
        resultMap.put("sj", sj);
        resultMap.put("twocx", twocx);
        resultMap.put("twocy",twocy);
        resultMap.put("twosj", twosj);
        resultMap.put("threecx", threecx);
        resultMap.put("threecy",threecy);
        resultMap.put("threesj", threesj);
        resultMap.put("fourcx", fourcx);
        resultMap.put("fourcy",fourcy);
        resultMap.put("foursj", foursj);




        return resultMap;
    }

    @GetMapping("/topfive")
    public  Map<String, String> topfive(CxcyAchievement cxcyAchievement) {


        List<CxcyAchievement> list = cxcyAchievementService.selectCxcyAchievementList(cxcyAchievement);

        Map<String, Integer> result = new HashMap<>();
        for (CxcyAchievement c : list) {
            String firstCollege = c.getFirstCollege();
            result.put(firstCollege, result.getOrDefault(firstCollege, 0) + 1);
        }

        List<Map.Entry<String, Integer>> entryList = new ArrayList<>(result.entrySet());

        Collections.sort(entryList, new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                return o2.getValue().compareTo(o1.getValue());
            }
        });

        Map<String, Integer> sortedResult = new LinkedHashMap<>();
        int count = 0;
        for (Map.Entry<String, Integer> entry : entryList) {
            sortedResult.put(entry.getKey(), entry.getValue());
            count++;
            if (count >= 5) {
                break;
            }
        }

        Map<String, String> collegeMap = new HashMap<>();
        collegeMap.put("001", "文学院");
        collegeMap.put("002", "马克思主义学院");
        collegeMap.put("003", "工学院");
        collegeMap.put("004", "理学院");
        collegeMap.put("005", "法学院");
        collegeMap.put("006", "历史文化学院");
        collegeMap.put("007", "物理科学与技术学院");
        collegeMap.put("008", "数学科学学院");
        collegeMap.put("009", "化学与材料工程学院");
        collegeMap.put("010", "食品科学与工程学院");
        collegeMap.put("011", "信息科学与技术学院");
        collegeMap.put("012", "控制科学与工程学院");
        collegeMap.put("013", "经济学院");
        collegeMap.put("014", "管理学院");
        collegeMap.put("015", "旅游学院");
        collegeMap.put("016", "外国语学院");
        collegeMap.put("017", "教育科学学院");
        collegeMap.put("018", "体育学院");
        collegeMap.put("019", "美术学院");
        collegeMap.put("020", "音乐学院");
        collegeMap.put("021", "新闻与传播学院");
        collegeMap.put("022", "航运学院");
        collegeMap.put("023", "大学外语教研部");
        collegeMap.put("024", "师范学院");
        collegeMap.put("025", "创新创业学院");
        collegeMap.put("026", "继续教育学院");
        collegeMap.put("027", "附属高级中学");
        collegeMap.put("028", "国际教育学院");




// 添加其他键值对...

        Map<String, String> finalResult = new LinkedHashMap<>();
        for (Map.Entry<String, Integer> entry : sortedResult.entrySet()) {
            String collegeName = collegeMap.getOrDefault(entry.getKey(), "");
            finalResult.put(collegeName, String.valueOf(entry.getValue()));
        }

        System.out.println(finalResult);



        return finalResult;
    }


    @GetMapping("/zhuantopfive")
    public  Map<String, String> zhuantopfive(CxcyTransformation cxcyTransformation) {

        cxcyTransformation.setAuditState("2");
        List<CxcyTransformation> list = cxcyTransformationService.selectCxcyTransformationList(cxcyTransformation);

        Map<String, Integer> result = new HashMap<>();
        for (CxcyTransformation c : list) {
            String firstCollege = c.getFirstCollege();
            result.put(firstCollege, result.getOrDefault(firstCollege, 0) + 1);
        }

        List<Map.Entry<String, Integer>> entryList = new ArrayList<>(result.entrySet());

        Collections.sort(entryList, new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                return o2.getValue().compareTo(o1.getValue());
            }
        });

        Map<String, Integer> sortedResult = new LinkedHashMap<>();
        int count = 0;
        for (Map.Entry<String, Integer> entry : entryList) {
            sortedResult.put(entry.getKey(), entry.getValue());
            count++;
            if (count >= 5) {
                break;
            }
        }

        Map<String, String> collegeMap = new HashMap<>();
        collegeMap.put("001", "文学院");
        collegeMap.put("002", "马克思主义学院");
        collegeMap.put("003", "工学院");
        collegeMap.put("004", "理学院");
        collegeMap.put("005", "法学院");
        collegeMap.put("006", "历史文化学院");
        collegeMap.put("007", "物理科学与技术学院");
        collegeMap.put("008", "数学科学学院");
        collegeMap.put("009", "化学与材料工程学院");
        collegeMap.put("010", "食品科学与工程学院");
        collegeMap.put("011", "信息科学与技术学院");
        collegeMap.put("012", "控制科学与工程学院");
        collegeMap.put("013", "经济学院");
        collegeMap.put("014", "管理学院");
        collegeMap.put("015", "旅游学院");
        collegeMap.put("016", "外国语学院");
        collegeMap.put("017", "教育科学学院");
        collegeMap.put("018", "体育学院");
        collegeMap.put("019", "美术学院");
        collegeMap.put("020", "音乐学院");
        collegeMap.put("021", "新闻与传播学院");
        collegeMap.put("022", "航运学院");
        collegeMap.put("023", "大学外语教研部");
        collegeMap.put("024", "师范学院");
        collegeMap.put("025", "创新创业学院");
        collegeMap.put("026", "继续教育学院");
        collegeMap.put("027", "附属高级中学");
        collegeMap.put("028", "国际教育学院");




// 添加其他键值对...

        Map<String, String> finalResult = new LinkedHashMap<>();
        for (Map.Entry<String, Integer> entry : sortedResult.entrySet()) {
            String collegeName = collegeMap.getOrDefault(entry.getKey(), "");
            finalResult.put(collegeName, String.valueOf(entry.getValue()));
        }

        System.out.println(finalResult);



        return finalResult;
    }






}
